package tester.component;

import tester.Utils.PubUtil;
import tester.common.Constants;
import tester.entitys.JdbcInfo;
import tester.entitys.KafkaInfo;

import java.util.*;

public class Configer {
    static Set<String> properties;
    static HashMap<String, JdbcInfo> JdbcMap;
    static HashMap<String, KafkaInfo> KafkaMap;
    public static final String prefix = "Configer";
    public static void config(List<String> lines) {
        init();
        for (String line : lines) {
            String prefixStr = PubUtil.getPrefix(line);
            String suffixStr = PubUtil.getSuffix(line);
            if (prefixStr == null) continue;
            String[] prefixs = prefixStr.split("\\.");
            if (!prefixs[0].equals(prefix)) continue;
            configEntity(prefixs, suffixStr);
        }
    }

    private static void configEntity(String[] prefixs, String suffixStr) {
        String signature = prefixs[2];
        String propertie = prefixs[3];
        switch (prefixs[1]) {
            case Constants.JdbcSource:
                if (!JdbcMap.containsKey(signature)) {
                    JdbcMap.put(signature, new JdbcInfo(null, null, null, signature));
                    properties.add(signature);
                }
                switch (propertie) {
                    case Constants.JdbcUrl:
                        JdbcMap.get(signature).setUrl(suffixStr);
                        break;
                    case Constants.JdbcUser:
                        JdbcMap.get(signature).setUser(suffixStr);
                        break;
                    case Constants.JdbcPwd:
                        JdbcMap.get(signature).setPwd(suffixStr);
                        break;
                    case Constants.JdbcTable:
                        JdbcMap.get(signature).setTable(suffixStr);
                        break;
                }
                break;
            case Constants.KafkaSource:
                if (!KafkaMap.containsKey(signature)) {
                    KafkaMap.put(signature, new KafkaInfo(null, null, null, signature));
                    properties.add(signature);
                }
                switch (propertie) {
                    case Constants.KafkaIps:
                        KafkaMap.get(signature).setIps(suffixStr);
                        break;
                }
                break;

        }
    }

    public static void init() {
        properties = new HashSet<>();
        JdbcMap = new HashMap<>();
        KafkaMap = new HashMap<>();
    }
}
